//
//  SNFoodSegLineView.m
//  project
//
//  Created by 黄建华 on 2018/11/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNFoodSegLineView.h"

@implementation SNFoodSegLineView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    [self removeAllSubviews];
    if (_segView) {
        [_segView removeFromSuperview];
        _segView = nil;
    }
    _segView                     = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 8)];
    _segView.backgroundColor     = [[UIColor grayColor]colorWithAlphaComponent:0.25];
    _segView.layer.cornerRadius  = _segView.height/2.0;
    _segView.layer.masksToBounds = YES;
    _segView.clipsToBounds       = YES;
    [self addSubview:_segView];
}

- (void)selectView:(NSInteger)index
{
    for (UIView *view in _segView.subviews) {
        if ([view isKindOfClass:[UIView class]]) {
            if (view.tag >999) {
                if (view.tag == index + 1000) {
                    view.backgroundColor = FOODCOLOR;
                }
                else
                {
                    view.backgroundColor = [UIColor clearColor];
                }
            }
        }
    }
}

- (void)updateView:(NSArray *)array withTotal:(NSInteger)totalCalor {
    [self createView];
    NSArray *arr = array;
    // 当前消耗的卡路里 总小于目标消耗
    NSInteger total = totalCalor;
    // 反之按比列
    NSInteger colorTotal = 0;
    NSInteger sss = 555;
    for (int k = 0; k < arr.count; k++) {
        colorTotal += [[arr objectAtIndex:k]integerValue];
        if (colorTotal >totalCalor) {
            sss = k;
        }
    }
    if (colorTotal > total) {
        total = colorTotal;
    }
    
    float space = 2.0;
    float min   = 20;
    
    float width_x = _segView.left;
    NSInteger changeS = 0;
    for (int i = 0; i < arr.count; i++) {
        float value = [[arr objectAtIndex:i]floatValue];
        float width = value/total*(_segView.width-arr.count *space);
//        if (width <= min) {
//            width = min;
//        }
        
        changeS += (NSInteger)value;
        UIView *view = [[UIView alloc] init];
        view.tag = 1000+ i;
        view.backgroundColor = FOODCOLOR;
        view.frame           = CGRectMake(width_x, 0, width, 8);
        [_segView addSubview:view];
        
//        if (changeS >totalCalor) {
//            NSInteger moreValue = changeS - totalCalor;
//            CGFloat moreWidth = 1.0*moreValue/value *width;
//            UIView *moreView = [[UIView alloc] init];
//            moreView.frame = CGRectMake(width - moreWidth, 0,moreWidth, 8);
//            [view addSubview:moreView];
//            moreView.backgroundColor = [UIColor redColor];
//        }
        
        if (i == sss) {
            [view addRectCorner:UIRectCornerTopRight|UIRectCornerBottomRight withSize:CGSizeMake(4, 4)];
        }
        
//        UILabel *label = [UILabel simpleWithRect:CGRectMake(view.left, view.bottom+5, width, self.height/2.0) withAlignment:NSTextAlignmentCenter withFontSize:10.0 withText:[NSString stringWithFormat:@"%.0f", value] withTextColor:[UIColor grayColor]];
//        label.font = SNFITFONT(10.0);
//        label.adjustsFontSizeToFitWidth = YES;
//        label.minimumScaleFactor = 0.25;
//
//        [self addSubview:label];
//        if (label.right >self.width && label.width == 20) {
//            label.frame = CGRectMake(self.width - 25, view.bottom+5,25, self.height/2.0);
//            label.textAlignment = NSTextAlignmentRight;
//        }
        
        UIView *spaceView = [[UIView alloc] init];
        spaceView.frame           = CGRectMake(view.right, view.top, space, view.height);
        spaceView.backgroundColor = [UIColor whiteColor];
        [_segView addSubview:spaceView];
        width_x = width_x+width+space;
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
